
/*
* 时间：2021.07.22
* 代码来源：公众号【从0到1的点云】
* 功能：实现AABB包围盒
*/
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/common/common.h>
#include <pcl/common/distances.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h> 
#include <pcl/filters/filter.h>

class AABB
{
public :
  AABB() :viewer(new pcl::visualization::PCLVisualizer),cloud(new pcl::PointCloud<pcl::PointXYZ>())
  {

  };
  void view()
{
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(this->cloud,0,255,0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "cloud");
    pcl::PointXYZ p1(min_point.x, min_point.y,min_point.z);
    pcl::PointXYZ p2(min_point.x, max_point.y,min_point.z);
    pcl::PointXYZ p3(max_point.x, max_point.y,min_point.z);
    pcl::PointXYZ p4(max_point.x, min_point.y,min_point.z);
    pcl::PointXYZ p5(min_point.x, min_point.y,max_point.z);
    pcl::PointXYZ p6(min_point.x, max_point.y,max_point.z);
    pcl::PointXYZ p7(max_point.z, max_point.y,max_point.z);
    pcl::PointXYZ p8(max_point.x, min_point.y,max_point.z);

    viewer->addLine(p1, p2, 255, 0, 0, "1");
    viewer->addLine(p2, p3, 255, 0, 0, "2");
    viewer->addLine(p3, p4, 255, 0, 0, "3");
    viewer->addLine(p4, p1, 255, 0, 0, "4");

    viewer->addLine(p5, p6, 255, 0, 0, "5");
    viewer->addLine(p6, p7, 255, 0, 0, "6");
    viewer->addLine(p7, p8, 255, 0, 0, "7");
    viewer->addLine(p8, p5, 255, 0, 0, "8");

    viewer->addLine(p1, p5, 255, 0, 0, "9");
    viewer->addLine(p2, p6, 255, 0, 0, "10");
    viewer->addLine(p3, p7, 255, 0, 0, "11");
    viewer->addLine(p4, p8, 255, 0, 0, "12");
    viewer->spin();

  };
  void compute()
{
    pcl::getMinMax3D(*(this->cloud),this->min_point,this->max_point);
  };
  void inputcloud( const pcl::PointCloud<pcl::PointXYZ>::Ptr &CloudPtr)
{
    cloud = CloudPtr;
  }
private:
  pcl::PointXYZ  min_point;
  pcl::PointXYZ  max_point;
  pcl::visualization::PCLVisualizer::Ptr viewer;
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
};


int main()
{
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
  pcl::PCDReader reader;
  reader.read("bunny.pcd",*cloud);
  std::vector<int> idx;
  pcl::removeNaNFromPointCloud(*cloud, *cloud,idx);
  AABB aabb;
  aabb.inputcloud(cloud);
  aabb.compute();
  aabb.view();
}